﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.WebComponent
Imports AugmentLib.InputOutput.Encryption

Partial Class RecentTransaction
    Inherits System.Web.UI.Page


#Region " Properties "

    Public Property CurrentPage() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPage As Object = ViewState("_CurrentPage")
            If objPage Is Nothing Then
                Return 0
            Else
                'default to showing the first page
                Return CInt(objPage)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_CurrentPage") = value
        End Set
    End Property

    Public Property PageSize() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPageSize As Object = ViewState("_PageSize")
            If objPageSize Is Nothing Then
                Return 25
            Else
                'default to showing the first page
                Return CInt(objPageSize)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_PageSize") = value
        End Set
    End Property

#End Region

#Region " Paging and Page Size "

    Protected Sub lnkHead10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead10Item.Click
        PageSize = 10
        LoadUpload()
    End Sub

    Protected Sub lnkHead25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead25Item.Click
        PageSize = 25
        LoadUpload()
    End Sub

    Protected Sub lnkHead100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead100Item.Click
        PageSize = 100
        LoadUpload()
    End Sub

    Protected Sub lnkHead150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead150Item.Click
        PageSize = 150
        LoadUpload()
    End Sub

    Protected Sub lnkFoot10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot10Item.Click
        PageSize = 10
        LoadUpload()
    End Sub

    Protected Sub lnkFoot25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot25Item.Click
        PageSize = 25
        LoadUpload()
    End Sub

    Protected Sub lnkFoot100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot100Item.Click
        PageSize = 100
        LoadUpload()
    End Sub

    Protected Sub lnkFoot150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot150Item.Click
        PageSize = 150
        LoadUpload()
    End Sub

    Protected Sub btnMoveFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveFirst.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
        LoadUpload()
    End Sub

    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        If CurrentPage > 0 Then
            CurrentPage = CurrentPage - 1
            txtPageNo.Text = CurrentPage + 1
            LoadUpload()
        End If
    End Sub

    Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
        If CInt(txtPageNo.Text < 1) Then
            txtPageNo.Text = 1
        End If

        If CInt(txtPageNo.Text > CInt(lblMaxPageCount.Text)) Then
            txtPageNo.Text = CInt(lblMaxPageCount.Text) + 1
        End If

        CurrentPage = CInt(txtPageNo.Text) - 1
        LoadUpload()

    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If CurrentPage < CInt(lblMaxPageCount.Text) Then
            CurrentPage = CurrentPage + 1
            txtPageNo.Text = CurrentPage + 1
            LoadUpload()
        End If
    End Sub

    Protected Sub btnMoveLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveLast.Click
        CurrentPage = CInt(lblMaxPageCount.Text)
        txtPageNo.Text = CurrentPage + 1
        LoadUpload()
    End Sub

#End Region



    Protected Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
        Session("anchor1") = ""
        LoadUpload()
    End Sub

    Protected Sub btnResetSearchFilter_Click(sender As Object, e As EventArgs) Handles btnResetSearchFilter.Click
        CurrentPage = 0
        txtAlamat.Text = String.Empty
        txtKawasan.Text = String.Empty
        txtProject.Text = String.Empty
        ddlDaerah.SelectedIndex = 0
        ddlTransactType.SelectedIndex = 0
        ddlLSK.SelectedIndex = 0
        ddlLandType.SelectedIndex = 0
        ddlPropertyType.SelectedIndex = 0
        txtLandAreaMin.Text = String.Empty
        txtLandAreaMax.Text = String.Empty
        txtBuildUpMin.Text = String.Empty
        txtBuildUpMax.Text = String.Empty
        txtPriceMin.Text = String.Empty
        txtPriceMax.Text = String.Empty
        txtTransactionDateMin.Text = String.Empty
        txtTransactionDateMax.Text = String.Empty
        ddlStatusDelete.SelectedIndex = 0
    End Sub

    Private Sub LoadUpload()
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable

        divError.Visible = False
        divSuccessful.Visible = False
        Try
            objDB.OpenConnection()

            Dim LandType, PropertyType, Alamat, Kawasan, Project, Daerah, LandAreaMin, LandAreaMax, LSK, TransactType,
                BuildUpMin, BuildUpMax, PriceMin, PriceMax, TransactionDateMin, TransactionDateMax As String

            If txtAlamat.Text <> String.Empty Then
                Alamat = " AND Alamat LIKE @Alamat"
                objDB.AddParameter("Alamat", Data.SqlDbType.NVarChar, "%" & txtAlamat.Text.Trim & "%")
            Else
                Alamat = String.Empty
            End If

            If txtKawasan.Text <> String.Empty Then
                Kawasan = " AND Kawasan LIKE @Kawasan"
                '" AND Alamat LIKE '%" + txtAlamat.Text.Replace(",", "%' AND [Title] LIKE '%") + "%'"
                objDB.AddParameter("Kawasan", Data.SqlDbType.NVarChar, "%" & txtKawasan.Text.Trim & "%")
            Else
                Kawasan = String.Empty
            End If

            If txtProject.Text <> String.Empty Then
                Project = " AND Project LIKE @Project"
                objDB.AddParameter("Project", Data.SqlDbType.NVarChar, "%" & txtProject.Text.Trim & "%")
            Else
                Project = String.Empty
            End If

            If ddlDaerah.SelectedValue <> "" Then
                Daerah = " AND Daerah LIKE @Daerah"
                objDB.AddParameter("Daerah", Data.SqlDbType.NChar, "%" & ddlDaerah.SelectedItem.Text.Trim & "%")
            Else
                Daerah = String.Empty
            End If

            If ddlTransactType.Text <> String.Empty Then
                TransactType = " AND TransactType LIKE @TransactType"
                objDB.AddParameter("TransactType", Data.SqlDbType.NVarChar, ddlTransactType.SelectedItem.Text.Trim)
            Else
                TransactType = String.Empty
            End If

            If ddlLSK.SelectedValue <> String.Empty Then
                LSK = " AND LSK = @LSK"
                objDB.AddParameter("LSK", Data.SqlDbType.TinyInt, ddlLSK.SelectedValue)
            Else
                LSK = String.Empty
            End If

            If ddlLandType.Text <> String.Empty Then
                LandType = " AND LandType LIKE @LandType"
                objDB.AddParameter("LandType", Data.SqlDbType.NVarChar, ddlLandType.SelectedItem.Text.Trim)
            Else
                LandType = String.Empty
            End If

            If ddlPropertyType.Text <> String.Empty Then
                PropertyType = " AND PropertyType LIKE @PropertyType"
                objDB.AddParameter("PropertyType", Data.SqlDbType.NVarChar, ddlPropertyType.SelectedItem.Text.Trim)
            Else
                PropertyType = String.Empty
            End If

            If txtLandAreaMin.Text.Trim <> String.Empty Then
                LandAreaMin = " AND Land>= @LandAreaMin"
                objDB.AddParameter("LandAreaMin", Data.SqlDbType.Decimal, txtLandAreaMin.Text.Trim)
            Else
                LandAreaMin = String.Empty
            End If

            If txtLandAreaMax.Text.Trim <> String.Empty Then
                LandAreaMax = " AND Land <= @LandAreaMax"
                objDB.AddParameter("LandAreaMax", Data.SqlDbType.Decimal, txtLandAreaMax.Text.Trim)
            Else
                LandAreaMax = String.Empty
            End If

            If txtBuildUpMin.Text.Trim <> String.Empty Then
                BuildUpMin = " AND BuildUp>= @BuildUpMin"
                objDB.AddParameter("BuildUpMin", Data.SqlDbType.Decimal, txtBuildUpMin.Text.Trim)
            Else
                BuildUpMin = String.Empty
            End If

            If txtBuildUpMax.Text.Trim <> String.Empty Then
                BuildUpMax = " AND Land <= @BuildUpMax"
                objDB.AddParameter("BuildUpMax", Data.SqlDbType.Decimal, txtBuildUpMax.Text.Trim)
            Else
                BuildUpMax = String.Empty
            End If

            If txtPriceMin.Text.Trim <> String.Empty Then
                PriceMin = " AND Price >= @PriceMin"
                objDB.AddParameter("PriceMin", Data.SqlDbType.Decimal, txtPriceMin.Text.Trim)
            Else
                PriceMin = String.Empty
            End If

            If txtPriceMax.Text.Trim <> String.Empty Then
                PriceMax = " AND Price <= @PriceMax"
                objDB.AddParameter("PriceMax", Data.SqlDbType.Decimal, txtPriceMax.Text.Trim)
            Else
                PriceMax = String.Empty
            End If

            If txtTransactionDateMin.Text.Trim <> String.Empty Then
                TransactionDateMin = " AND TransactionDate >= @TransactionDateMin"
                objDB.AddParameter("TransactionDateMin", Data.SqlDbType.Date, txtTransactionDateMin.Text.Trim)
            Else
                TransactionDateMin = String.Empty
            End If

            If txtTransactionDateMax.Text.Trim <> String.Empty Then
                TransactionDateMax = " AND TransactionDate <= @TransactionDateMax"
                objDB.AddParameter("TransactionDateMax", Data.SqlDbType.Date, txtTransactionDateMax.Text.Trim)
            Else
                TransactionDateMax = String.Empty
            End If

            objDB.AddParameter("StatusDelete", Data.SqlDbType.TinyInt, ddlStatusDelete.SelectedValue)

            objDB.dbVariable.SQLString = "SELECT TranId,LandType,PropertyType,Alamat,Kawasan,Project,Daerah" & _
                                        ",Price,TransactionDate,Land,BuildUp,Bedroom,LSK,TransactType from AW_RecentRentSale " & _
                                        "WHERE StatusDelete = @StatusDelete " & _
                                        LandType & PropertyType & Alamat & Kawasan & Project & Daerah & _
                                         LSK & TransactType & LandAreaMin & LandAreaMax & BuildUpMin & BuildUpMax & PriceMin & _
                                        PriceMax & TransactionDateMin & TransactionDateMax & _
                                        lblhidSortType.Text

            objDB.FillData(TempDT, "AW_RecentRentSale", Database.SQLCommandType.NormalString)

            objDB.CloseConnection()

            'Creating a Paged Datasouce
            Dim objPaged As New PagedDataSource
            objPaged.DataSource = TempDT.DefaultView
            objPaged.AllowPaging = True
            objPaged.PageSize = PageSize
            objPaged.CurrentPageIndex = CurrentPage

            lblHeadNoOfItem.Text = TempDT.Rows.Count
            lblFootNoOfItem.Text = TempDT.Rows.Count

            repUpload.DataSource = objPaged
            repUpload.DataBind()

            lblMaxPageCount.Text = objPaged.PageCount - 1
            lblTotalPage.Text = " of " & objPaged.PageCount

        Catch ex As Exception
            divError.Visible = True
            lblMsgError.Text = ex.Message.ToString
        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try
    End Sub

    Private Function SortType(ByVal strSortType As String) As String
        Dim strSort As String
        If Right(strSortType, 1) = "▼" Then
            strSort = "ASC"
        Else
            strSort = "DESC"
        End If
        Return strSort
    End Function

    Protected Sub lnkAlamat_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkAlamat.Click
        If lnkAlamat.Text = "Alamat▼" Then
            lnkAlamat.Text = "Alamat▲"
        Else
            lnkAlamat.Text = "Alamat▼"
        End If

        lblhidSortType.Text = " ORDER BY Alamat " & SortType(lnkAlamat.Text) & _
                            ", TransactionDate " & SortType(lnkTransactionDate.Text) & _
                            ", Kawasan " & SortType(lnkKawasan.Text) & _
                            ", Project " & SortType(lnkProject.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkKawasan_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkKawasan.Click
        If lnkKawasan.Text = "Kawasan▼" Then
            lnkKawasan.Text = "Kawasan▲"
        Else
            lnkKawasan.Text = "Kawasan▼"
        End If

        lblhidSortType.Text = " ORDER BY Kawasan " & SortType(lnkKawasan.Text) & _
                            ", TransactionDate " & SortType(lnkTransactionDate.Text) & _
                            ", Alamat " & SortType(lnkAlamat.Text) & _
                            ", Project " & SortType(lnkProject.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkProject_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkProject.Click
        If lnkProject.Text = "Project▼" Then
            lnkProject.Text = "Project▲"
        Else
            lnkProject.Text = "Project▼"
        End If

        lblhidSortType.Text = " ORDER BY Project " & SortType(lnkProject.Text) & _
                            ", TransactionDate " & SortType(lnkTransactionDate.Text) & _
                            ", Alamat " & SortType(lnkAlamat.Text) & _
                            ", Daerah " & SortType(lnkDaerah.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkDaerah_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkDaerah.Click
        If lnkDaerah.Text = "Daerah▼" Then
            lnkDaerah.Text = "Daerah▲"
        Else
            lnkDaerah.Text = "Daerah▼"
        End If

        lblhidSortType.Text = " ORDER BY Daerah " & SortType(lnkDaerah.Text) & _
                            ", TransactionDate " & SortType(lnkTransactionDate.Text) & _
                            ", Alamat " & SortType(lnkAlamat.Text) & _
                            ", Project " & SortType(lnkProject.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkLSK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLSK.Click
        If lnkLSK.Text = "LSK▼" Then
            lnkLSK.Text = "LSK▲"
        Else
            lnkLSK.Text = "LSK▼"
        End If

        lblhidSortType.Text = " ORDER BY LSK " & SortType(lnkLSK.Text) & _
                            ", TransactionDate " & SortType(lnkTransactionDate.Text) & _
                            ", Alamat " & SortType(lnkAlamat.Text) & _
                            ", Project " & SortType(lnkProject.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkLandType_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLandType.Click
        If lnkLandType.Text = "Land Type▼" Then
            lnkLandType.Text = "Land Type▲"
        Else
            lnkLandType.Text = "Land Type▼"
        End If

        lblhidSortType.Text = " ORDER BY LandType " & SortType(lnkLandType.Text) & _
                            ", TransactionDate " & SortType(lnkTransactionDate.Text) & _
                            ", Alamat " & SortType(lnkAlamat.Text) & _
                            ", Project " & SortType(lnkProject.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkPropertyType_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkPropertyType.Click
        If lnkPropertyType.Text = "Property Type▼" Then
            lnkPropertyType.Text = "Property Type▲"
        Else
            lnkPropertyType.Text = "Property Type▼"
        End If

        lblhidSortType.Text = " ORDER BY Property " & SortType(lnkPropertyType.Text) & _
                            ", TransactionDate " & SortType(lnkTransactionDate.Text) & _
                            ", Alamat " & SortType(lnkAlamat.Text) & _
                            ", Project " & SortType(lnkProject.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkTransactionDate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkTransactionDate.Click
        If lnkTransactionDate.Text = "Transaction Date▼" Then
            lnkTransactionDate.Text = "Transaction Date▲"
        Else
            lnkTransactionDate.Text = "Transaction Date▼"
        End If

        lblhidSortType.Text = " ORDER BY TransactionDate " & SortType(lnkTransactionDate.Text) & _
                            ", Alamat " & SortType(lnkAlamat.Text) & _
                            ", Project " & SortType(lnkProject.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkPrice_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkPrice.Click
        If lnkPrice.Text = "Price▼" Then
            lnkPrice.Text = "Price▲"
        Else
            lnkPrice.Text = "Price▼"
        End If

        lblhidSortType.Text = " ORDER BY Price " & SortType(lnkPrice.Text) & _
                            ", TransactionDate " & SortType(lnkTransactionDate.Text) & _
                            ", Alamat " & SortType(lnkAlamat.Text) & _
                            ", Project " & SortType(lnkProject.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkLand_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLand.Click
        If lnkLand.Text = "Land▼" Then
            lnkLand.Text = "Land▲"
        Else
            lnkLand.Text = "Land▼"
        End If

        lblhidSortType.Text = " ORDER BY Land " & SortType(lnkLand.Text) & _
                            ", TransactionDate " & SortType(lnkTransactionDate.Text) & _
                            ", Alamat " & SortType(lnkAlamat.Text) & _
                            ", Project " & SortType(lnkProject.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub repUpload_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repUpload.ItemCommand
        If e.CommandName = "ViewList" Then
            '1-Back Page
            '2-Page Size
            '3-Current Page

            Dim strBack As String
            strBack = "RecentTransaction.aspx" & "ڡ" & PageSize & "ڡ" & CurrentPage & "ڡ" & txtAlamat.Text.Trim & "ڡ" & _
                    txtKawasan.Text.Trim & "ڡ" & txtProject.Text.Trim & "ڡ" & ddlDaerah.SelectedValue.Trim & "ڡ" & ddlTransactType.SelectedValue.Trim & "ڡ" & _
                    ddlLSK.SelectedValue.Trim & "ڡ" & ddlLandType.SelectedValue.Trim & "ڡ" & ddlPropertyType.SelectedValue.Trim & "ڡ" & txtLandAreaMin.Text.Trim & "ڡ" & _
                    txtLandAreaMax.Text.Trim & "ڡ" & txtBuildUpMin.Text.Trim & "ڡ" & txtBuildUpMax.Text.Trim & "ڡ" & txtPriceMin.Text.Trim & "ڡ" & _
                    txtPriceMax.Text.Trim & "ڡ" & txtTransactionDateMin.Text.Trim & "ڡ" & txtTransactionDateMax.Text.Trim & "ڡ" & ddlStatusDelete.SelectedValue.Trim

            Session.Add("Back", strBack)
            'Response.Redirect("AuctionEntry.aspx?id=" & SymmetricEncryptToHex(e.CommandArgument, "augment"))
            Response.Redirect("RecentTransactionEntry.aspx?id=" & e.CommandArgument)
        End If
        If e.CommandName.ToUpper = "DELETEITEM" Then

            'Begin Delete
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Try
                objDB.OpenConnection()
                objDB.dbVariable.SQLString = "UPDATE AW_RecentRentSale SET StatusDelete=1 WHERE TranId=@TranId"
                objDB.AddParameter("TranId", Data.SqlDbType.UniqueIdentifier, New Guid(CType(repUpload.Items(e.CommandArgument).FindControl("lblRepTranId"), Label).Text))
                objDB.ExecuteQuery()
                objDB.CloseConnection()
                lblMsgSuccessful.Text = "Entry Deleted"
                lblMsgSuccessful.Visible = True
                divSuccessful.Visible = True
                LoadUpload()
            Catch ex As Exception
                divError.Visible = True
                lblMsgError.Text = ex.Message.ToString
            Finally
                If (objDB IsNot Nothing) Then
                    objDB.CloseConnection()
                End If
            End Try
        End If
    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Session.Item("StaffId") Is Nothing Then
            Response.Redirect("default.aspx")
        End If

        'Edited by minginn @ 27/6/2011 - to load the same search when user back from other page
        If Not Session("Back") Is Nothing Then
            Dim strBack As String = Session("Back").ToString
            Dim arr() As String = strBack.Split("ڡ")

            If arr(0).ToString.Contains("RecentTransaction.aspx") Then
                PageSize = CInt(arr(1).ToString)
                CurrentPage = CInt(arr(2).ToString)
                txtAlamat.Text = arr(3).ToString
                txtKawasan.Text = arr(4).ToString
                txtProject.Text = arr(5).ToString
                ddlDaerah.SelectedValue = arr(6).ToString
                ddlTransactType.SelectedValue = arr(7).ToString
                ddlLSK.SelectedValue = arr(8).ToString
                ddlLandType.SelectedValue = arr(9).ToString
                ddlPropertyType.SelectedValue = arr(10).ToString
                txtLandAreaMin.Text = arr(11).ToString
                txtLandAreaMax.Text = arr(12).ToString
                txtBuildUpMin.Text = arr(13).ToString
                txtBuildUpMax.Text = arr(14).ToString
                txtPriceMin.Text = arr(15).ToString
                txtPriceMax.Text = arr(16).ToString
                txtTransactionDateMin.Text = arr(17).ToString
                txtTransactionDateMax.Text = arr(18).ToString
                ddlStatusDelete.SelectedValue = arr(19).ToString
                Session.Remove("Back")
                LoadUpload()

            Else
                Session.Remove("Back")
            End If
        Else
            If Not Page.IsPostBack Then

                lblStaffUID.Text = Session.Item("StaffId").ToString
                lblStaffType.Text = Session.Item("StaffType").ToString
                lnkTransactionDate_Click(sender, e)
            End If
        End If

        If Not Page.IsPostBack Then

            lblStaffUID.Text = Session.Item("StaffId").ToString
            lblStaffType.Text = Session.Item("StaffType").ToString

        End If
    End Sub

End Class
